package model;

/**
 * <p>An enumeration for the six possible neighbors of a hexagonal field. The
 * elements have convenient methods to find the next, previous and opposite
 * direction.</p>
 * <p>The hex fields are ordered clockwise, beginning with the topmost.</p>
 * @author Jan-Philipp Kappmeier
 */
public enum HexDirection {
	/** The (unique) field on above an hex field. */
	Upper,
	/** The upper of the two fields on the right side. */
	UpperRight,
	/** The lower of the two fields on the right side. */
	LowerRight,
	/** The (unique) field below an hex field. */
	Lower,
	/** The lower of the two fields on the left side. */
	LowerLeft,
	/** The upper of the two fields on the left side. */
	UpperLeft;

	/**
	 * Returns the opposite direction.
	 * @return the opposite direction
	 * @throws IllegalStateException if any direction is not implemented
	 */
	public HexDirection opposite() throws IllegalStateException {
		switch( this ) {
			case Upper:
				return Lower;
			case UpperRight:
				return LowerLeft;
			case LowerRight:
				return UpperLeft;
			case Lower:
				return Upper;
			case LowerLeft:
				return UpperRight;
			case UpperLeft:
				return LowerRight;
		}
		throw new IllegalStateException("Direction does not exist." );
	}

	/**
	 * Returns the next direction (which is right of the instance or in clockwise
	 * order).
	 * @return the next direction
	 * @throws IllegalStateException if any direction is not implemented
	 */
	public final HexDirection next() throws IllegalStateException {
		switch( this ) {
			case Upper:
				return UpperRight;
			case UpperRight:
				return LowerRight;
			case LowerRight:
				return Lower;
			case Lower:
				return LowerLeft;
			case LowerLeft:
				return UpperLeft;
			case UpperLeft:
				return Upper;
		}
		throw new IllegalStateException("Direction does not exist." );
	}

	/**
	 * Returns the previous direction (left of the instances direction or piece
	 * clockwise).
	 * @return the previous direction
	 * @throws IllegalStateException if any direction is not implemented
	 */
	public final HexDirection previous() throws IllegalStateException {
		switch( this ) {
			case Upper:
				return UpperLeft;
			case UpperRight:
				return Upper;
			case LowerRight:
				return UpperRight;
			case Lower:
				return LowerRight;
			case LowerLeft:
				return Lower;
			case UpperLeft:
				return LowerLeft;
		}
		throw new IllegalStateException("Direction does not exist." );
	}
}
